/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_HAL_SPI_H__
#define __PONY_HAL_SPI_H__

/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/
#include "hal.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/
#define HAL_SPI_MODE_MASTER 0x01 /* spi communication is master mode */
#define HAL_SPI_MODE_SLAVE  0x02 /* spi communication is slave mode */

#define HAL_SPI_CMD_ENABLE  0x01
#define HAL_SPI_CMD_DISABLE 0x02

/* typedefs ------------------------------------------------------------------*/

typedef struct hal_spi hal_spi_t;

typedef struct hal_spi_methods {
    int (*write)(hal_spi_t *, const void *, size_t, uint32_t);
    int (*read)(hal_spi_t *, void *, size_t, uint32_t);
    int (*write_read)(hal_spi_t *, const void *, void *, size_t, uint32_t);
    int (*ioctl)(hal_spi_t *, int cmd, va_list valist);
} hal_spi_methods_t;

typedef struct hal_spi {
    hal_dev_t parent;
    k_mutex_t lock;
    uint8_t mode;
    const hal_spi_methods_t *methods;
} hal_spi_t;

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern hal_spi_t *hal_spi_open(const char *name);
extern int hal_spi_write(hal_spi_t *spi, const void *buf, size_t len, uint32_t timeout);
extern int hal_spi_read(hal_spi_t *spi, void *buf, size_t len, uint32_t timeout);
extern int hal_spi_write_read(hal_spi_t *spi, const void *txbuf, void *rxbuf, size_t len,
                              uint32_t timeout);
extern int hal_spi_ioctl(hal_spi_t *spi, int cmd, ...);
extern int hal_spi_register(hal_spi_t *spi, const char *name, const hal_spi_methods_t *methods,
                            uintptr_t data);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_HAL_SPI_H__ */
